import 'dotenv/config'
import { Redis } from 'ioredis'

const {
    REDIS_HOST: host,
    REDIS_PORT: port,
    REDIS_PASSWORD: password,
} = process.env;

export async function redisGet(key: string) {
    const instance = await RedisService.getInstance();
    return await instance.get(key)
}


export async function redisSet(key: string, value: any, expire?: number) {
    const instance = await RedisService.getInstance();
    await instance.set(key, value)
    expire && await instance.expire(key, expire - 10)
}

class RedisService {
    private static instance: Redis;

    public static async getInstance() {
        if (!this.instance) {
            this.instance = new Redis({ host, port: +`${port}`, password })
        }
        return this.instance;
    }
}